package com.uyayo.ucenter.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.framework.common.exception.CodeMultipleException;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import com.uyayo.ucenter.entity.PermissionInit;
import com.uyayo.ucenter.mapper.PermissionInitMapper;
import com.uyayo.ucenter.model.PermissionInitCondition;
import com.uyayo.ucenter.service.PermissionInitService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("permissionInitService")
public class PermissionInitServiceImpl implements PermissionInitService {

	@Resource
	private PermissionInitMapper permissionInitMapper;

	@Override
	public PermissionInit getPermissionInitById(long id){
		return permissionInitMapper.selectByPrimaryKey(id);
	}

	@Override
	public PageBean<PermissionInit> selectPermissionInit(PageBean<PermissionInit> pageBean,PermissionInitCondition permissionInitCondition) {
		
		List<PermissionInit> permissionInit;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("code", "code"));
		orderBies.add(new OrderBy("name", "name"));
		orderBies.add(new OrderBy("description", "description"));
		orderBies.add(new OrderBy("createTime", "create_time"));
		orderBies.add(new OrderBy("updateTime", "update_time"));
		orderBies.add(new OrderBy("remark", "remark"));
		OrderByUtil orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, pageBean.getOrderField(), pageBean.getOrderDirection());
		PageHelper.startPage(pageBean.getPageCurrent(), pageBean.getPageSize());
		Example example = new Example(PermissionInit.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(permissionInitCondition.getUrl())){
			criteria.andLike("url","%"+permissionInitCondition.getUrl()+"%");
		}
		if(permissionInitCondition.getBeginCreateTime()!=null || permissionInitCondition.getEndCreateTime()!=null){
			criteria.andBetween("createTime", permissionInitCondition.getBeginCreateTime(), permissionInitCondition.getEndCreateTime());
		}
		permissionInit = permissionInitMapper.selectByExample(example);
		
		//取记录总条数
		PageInfo<PermissionInit> pageInfo = new PageInfo<>(permissionInit);
		
		return new PageBean<>(pageInfo.getTotal(), pageBean.getPageCurrent(), pageBean.getPageSize(), permissionInit);
	}

	@Override
	public List<PermissionInit> queryAll(){
		return permissionInitMapper.queryAll();
	}

	@Override
	public List<PermissionInit> selectPermissionInit(PermissionInit PermissionInit) {
		return permissionInitMapper.select(PermissionInit);
	}

	@Override
	public int savePermissionInit(PermissionInit permissionInit) throws CodeMultipleException {
        PermissionInit ParamTest = getPermissionInitByCode(permissionInit.getCode());
        if(ParamTest!=null){
            throw new CodeMultipleException("Code multiple error!");
        }
		return permissionInitMapper.insertSelective(permissionInit);
	}

	@Override
	public int updatePermissionInit(PermissionInit permissionInit) throws CodeMultipleException {
		PermissionInit permissionInitTest = getPermissionInitByCode(permissionInit.getCode());
		if(permissionInitTest!=null && !Objects.equals(permissionInit.getId(),permissionInitTest.getId())){
			throw new CodeMultipleException("Code multiple error!");
		}
		return permissionInitMapper.updateByPrimaryKeySelective(permissionInit);
	}

	@Override
	public int deletePermissionInitById(Long id) {
		return permissionInitMapper.deleteByPrimaryKey(id);
	}

	@Override
	public PermissionInit getPermissionInitByCode(String code) {
		PermissionInit permissionInit = new PermissionInit();
		permissionInit.setCode(code);
		permissionInit = permissionInitMapper.selectOne(permissionInit);
		return permissionInit;
	}

	@Override
	public PermissionInit getPermissionInitByEnabled(){
		PermissionInit permissionInit = new PermissionInit();
		permissionInit.setStatus(1);
		List<PermissionInit> Servers =  permissionInitMapper.select(permissionInit);
		return Servers.get(0);
	}
}
